$(function() {
	// 显示审核状态
	if ($("#status").val() != 0) {
		$("#approveStatus").show();
	}
	if ($("#status").val() == 3) {
		$("#approveRemark").show();
	}
	var index = $("#index").val();
	if (index != "") {
		$(".category li").eq(index).addClass("on").siblings().removeClass("on");
		$(".category_con table").eq(index).show().siblings().hide();
	} else {
		$(".menu_list").find(".lion").removeClass("lion");
		// $(".menu_list").find("li:eq(2)").addClass("lion");
		$("#enterprisemanager").addClass("lion");

		// 设置企业人数、行业、性质
		$("#industry").val($("#industryPara").val());
		$("#nature").val($("#naturePara").val());
		$("#scale").val($("#scalePara").val());
	}

	tab("category li", "category_con table", "on");

});
// tab切换
function tab(menuclass, tabclass, onclass) {
	$("." + menuclass).click(function() {
				$(this).addClass(onclass).siblings().removeClass(onclass);
				var index = $("." + menuclass).index(this);
				$("." + tabclass).eq(index).show().siblings().hide();
			});
}
function update() {
	var index = $('.category li').closest('.on').index();
	// 当前是企业基本信息
	if (index == 0) {
		if (!verifyInputField("name,address"))
			return;

		if ($("#scale").val() == "-1") {
			alertMsg("请选择公司人数");
			return;
		}
		if ($("#industry").val() == "-1") {
			alertMsg("请选择公司行业");
			return;
		}
		if ($("#nature").val() == "-1") {
			alertMsg("请选择公司性质");
			return;
		}

		var enterprise = {
			id : $("#recordId").val(),
			name : $('#name').val(),
			address : $('#address').val(),
			website : $('#website').val(),
			scale : $('#scale').val(),
			industry : $('#industry').val(),
			nature : $('#nature').val()
		};

		// console.log(enterprise);
		// 异步修改企业信息
		$.ajax({
					type : "POST",
					url : "usercenter/enterprisemanager/update_enterprise",
					dataType : "json",
					data : JSON.stringify(enterprise),
					contentType : "application/json;charset=UTF-8",
					success : function(data) {
						if (data.result === true) {
							// alertMsg("修改成功!");
							alertSuccMsgDialog("修改成功!");
						} else {
							// alertMsg("修改失败，" + data.result + "!");
							alertErrorMsgDialog("修改失败，" + data.result + "!");
						}
					}
				});

	} else if (index == 1) { // 当前是增值税发票
		confirmModifyMsg("此操作会使企业增票资质重新进入待审核状态，确认继续？","");
	}
}

function modifyApprove(){
	if (!verifyInputField("entName,taxpayerId,registeredAddress,registeredPhone,bank,bankAccount"))
			return;

		if (!checkTel("registeredPhone")) {
			return;
		}

		if ($("#attachmentFileName").val() == "") {
			alertMsg("请上传营业执照副本!");
			return;
		}

		if ($("#attachmentFileName1").val() == "") {
			alertMsg("请上传税务登记证副本!");
			return;
		}

		if ($("#attachmentFileName2").val() == "") {
			alertMsg("请上传银行开户许可证!");
			return;
		}

		if ($("#status").val() == 1) {
			alertErrorMsgDialog("未审核的增票资质不能修改!");
			return;
		}
		var vatDto = {
			id : $("#vatDtoId").val(),
			entName : $("#entName").val(),
			taxpayerId : $("#taxpayerId").val(),
			registeredAddress : $("#registeredAddress").val(),
			registeredPhone : $("#registeredPhone").val(),
			bank : $("#bank").val(),
			status : $("#status").val(),
			bankAccount : $("#bankAccount").val(),
			businessLicenseFile : $("#attachmentFileName").val(),
			taxRegistrationCertificateFile : $("#attachmentFileName1").val(),
			bankAccountPermitsFile : $("#attachmentFileName2").val()
		};

		$.ajax({
					type : "POST",
					url : "usercenter/enterprisemanager/update_vat",
					dataType : "json",
					data : JSON.stringify(vatDto),
					contentType : "application/json;charset=UTF-8",
					success : function(data) {
						if (data.result === true) {
							alertDialog("修改成功!", "succeed");
						} else {
							alertDialog("修改失败，" + data.result + "!", "error");
						}
					}
				});

}


var contextpath = $("#hid_contextpath").val();
var imgSize = 500 * 1024;
$('#attachment_file_upload').uploadify({
	'uploader' : contextpath
			+ '/ipiinclude/ipiplugin/uploadify/scripts/uploadify.swf',
	'script' : contextpath + '/file/upload;jsessionid='
			+ $("#session_id").val(),
	'cancelImg' : contextpath + '/ipiinclude/ipiplugin/uploadify/cancel.png',
	'buttonImg' : contextpath
			+ '/ipiinclude/ipiplugin/uploadify/upload_pitcure.png',
	'sizeLimit' : imgSize,
	'multi' : false,
	'auto' : true,
	'fileDesc' : '*.jpg',
	'fileExt' : '*.jpg',
	'queueID' : 'custom-queue',
	'queueSizeLimit' : 1,
	'simUploadLimit' : 1,
	'removeCompleted' : false,
	'onOpen' : function(event, queueId, fileObj) {
	},
	'onSelectOnce' : function(event, data) {
		$("#hasUploadAttachmentFile").val(1);
	},
	'onComplete' : function(event, queueId, fileObj, response, data) {
		response = eval('(' + response + ')');

		if (response.uploadFlag == true) {
			alertMsg('图片上传成功');
			// 上传成功，删除原来的图像
			// if($("#attachmentFileName1").val() != "")
			// deleteFlie($("#attachmentFileName1").val());

			$("#attachmentFilePath").val(response.filePath);
			$("#attachmentFileName").val(response.fileName);

			// 设置图片下载
			var href = "" + contextpath + "/ipiimg/userimg/"
					+ response.fileName;
			$("#att").html('已上传 &nbsp;&nbsp;<a target="_blank" href="' + href
					+ '">查看</a>');
		} else {
			alertMsg(response.errerMsg);
		}
	},
	'onError' : function(event, ID, fileObj, errorObj) {
		if (fileObj.size > imgSize) {
			alertMsg('上传图片超过最大限制！');
		} else if (errorObj.type == "HTTP" && errorObj.info == "404") {
			alertMsg('图片只支持jpg格式，请重现上传！');
		} else {
			alertMsg('上传图片失败，请稍后重试');
		}
	},
	'onCancel' : function(event, ID, fileObj, errorObj) {
		deleteFlie($("#attachmentFileName").val());
		$("#attachmentFilePath").val("");
		$("#attachmentFileName").val("");
		$("#hasUploadAttachmentFile").val(0);
	},
	'onClearQueue' : function(event, ID, fileObj, errorObj) {
	}
});

$('#attachment_file_upload1').uploadify({
	'uploader' : contextpath
			+ '/ipiinclude/ipiplugin/uploadify/scripts/uploadify.swf',
	'script' : contextpath + '/file/upload;jsessionid='
			+ $("#session_id").val(),
	'cancelImg' : contextpath + '/ipiinclude/ipiplugin/uploadify/cancel.png',
	'buttonImg' : contextpath
			+ '/ipiinclude/ipiplugin/uploadify/upload_pitcure.png',
	'sizeLimit' : imgSize,
	'multi' : false,
	'auto' : true,
	'fileDesc' : '*.jpg',
	'fileExt' : '*.jpg',
	'queueID' : 'custom-queue',
	'queueSizeLimit' : 1,
	'simUploadLimit' : 1,
	'removeCompleted' : false,
	'onOpen' : function(event, queueId, fileObj) {
	},
	'onSelectOnce' : function(event, data) {
		$("#hasUploadAttachmentFile1").val(1);
	},
	'onComplete' : function(event, queueId, fileObj, response, data) {
		response = eval('(' + response + ')');

		if (response.uploadFlag == true) {
			alertMsg('图片上传成功');
			// 上传成功，删除原来的图像
			// if($("#attachmentFileName1").val() != "")
			// deleteFlie($("#attachmentFileName1").val());

			$("#attachmentFilePath1").val(response.filePath);
			$("#attachmentFileName1").val(response.fileName);

			// 设置图片下载
			var href = "" + contextpath + "/ipiimg/userimg/"
					+ response.fileName;
			$("#att1").html('已上传 &nbsp;&nbsp;<a target="_blank" href="' + href
					+ '">查看</a>');
		} else {
			alertMsg(response.errerMsg);
		}

	},
	'onError' : function(event, ID, fileObj, errorObj) {
		// console.log("why not comce in ...");
		if (fileObj.size > imgSize) {
			alertMsg('上传图片超过最大限制！');
		} else if (errorObj.type == "HTTP" && errorObj.info == "404") {
			alertMsg('图片只支持jpg格式，请重现上传！');
		} else {
			alertMsg('上传图片失败，请稍后重试');
		}
	},
	'onCancel' : function(event, ID, fileObj, errorObj) {
		deleteFlie($("#attachmentFileName1").val());
		$("#attachmentFilePath1").val("");
		$("#attachmentFileName1").val("");
		$("#hasUploadAttachmentFile1").val(0);
	},
	'onClearQueue' : function(event, ID, fileObj, errorObj) {
	}
});

$('#attachment_file_upload2').uploadify({
	'uploader' : contextpath
			+ '/ipiinclude/ipiplugin/uploadify/scripts/uploadify.swf',
	'script' : contextpath + '/file/upload;jsessionid='
			+ $("#session_id").val(),
	'cancelImg' : contextpath + '/ipiinclude/ipiplugin/uploadify/cancel.png',
	'buttonImg' : contextpath
			+ '/ipiinclude/ipiplugin/uploadify/upload_pitcure.png',
	'sizeLimit' : imgSize,
	'multi' : false,
	'auto' : true,
	'fileDesc' : '*.jpg',
	'fileExt' : '*.jpg',
	'queueID' : 'custom-queue',
	'queueSizeLimit' : 1,
	'simUploadLimit' : 1,
	'removeCompleted' : false,
	'onOpen' : function(event, queueId, fileObj) {
	},
	'onSelectOnce' : function(event, data) {
		$("#hasUploadAttachmentFile2").val(1);
	},
	'onComplete' : function(event, queueId, fileObj, response, data) {
		response = eval('(' + response + ')');

		if (response.uploadFlag == true) {
			alertMsg('图片上传成功');
			// 上传成功，删除原来的图像
			// if($("#attachmentFileName1").val() != "")
			// deleteFlie($("#attachmentFileName1").val());

			$("#attachmentFilePath2").val(response.filePath);
			$("#attachmentFileName2").val(response.fileName);

			// 设置图片下载
			var href = "" + contextpath + "/ipiimg/userimg/"
					+ response.fileName;
			$("#att2").html('已上传 &nbsp;&nbsp;<a target="_blank" href="' + href
					+ '">查看</a>');
		} else {
			alertMsg(response.errerMsg);
		}
	},
	'onError' : function(event, ID, fileObj, errorObj) {
		if (fileObj.size > imgSize) {
			alertMsg('上传图片超过最大限制！');
		} else if (errorObj.type == "HTTP" && errorObj.info == "404") {
			alertMsg('图片只支持jpg格式，请重现上传！');
		} else {
			alertMsg('上传图片失败，请稍后重试');
		}
	},
	'onCancel' : function(event, ID, fileObj, errorObj) {
		deleteFlie($("#attachmentFileName2").val());
		$("#attachmentFilePath2").val("");
		$("#attachmentFileName2").val("");
		$("#hasUploadAttachmentFile2").val(0);
	},
	'onClearQueue' : function(event, ID, fileObj, errorObj) {
	}
});

function deleteFlie(fileName) {
	$.post("file/delete", {
				'fileName' : fileName
			});
}

function alertDialog(message, icon) {
	var list = art.dialog.list;
	for (var i in list) {
		list[i].close();
	};
	art.dialog({
		icon : icon,
		esc : true,
		content : message,
		lock : true,
		fixed : true,
		drag : false,
		ok : true,
		init : function() {
			var that = this, i = 10;
			var fn = function() {
				that.title(i + '秒后自动关闭');
				!i && that.close();
				i--;
			};
			tip_timer = setInterval(fn, 1000);
			fn();
		},
		close : function() {
			clearInterval(tip_timer);
			document.location.href = getRootUrl()
					+ "usercenter/enterprisemanager/index?index=1";
		}
	}).time(10);
}
function confirmModifyMsg(message) {
	var list = art.dialog.list;
	for (var i in list) {
		list[i].close();
	};
	art.dialog({
				esc : true,
				content : message,
				lock : true,
				fixed : true,
				drag : false,
				ok : function(){modifyApprove();},
				cancel : true,
				okVal : "继续",
				cancelVal : "取消",
				init : function() {
					var that = this, i = 10;
					var fn = function() {
						that.title(i + '秒后自动关闭');
						!i && that.close();
						i--;
					};
					tip_timer = setInterval(fn, 1000);
					fn();
				},
				close : function() {
					clearInterval(tip_timer);
				}
			}).time(10);
}